package com.thinkfit.jy.utils;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.common.lib.bean.ApplicationBean;
import com.thinkfit.jy.R;

import java.util.ArrayList;

/**
 * 选择关闭应用弹窗
 */
public class DialogCloseApkViewUtils implements View.OnClickListener {

    private static DialogCloseApkViewUtils dialogViewUtils;
    WindowManager windowManager;

    boolean isFist = true;
    TextView tvTitle,tvInfo,tvLeft,tvRight;
    ImageView iv1,iv2,iv3;
    ConstraintLayout clBg;
    Context mContext;
    View inflate;
    String packageName,openPackageName;
    ArrayList<ImageView> ivs = new ArrayList<>();
    ArrayList<ApplicationBean> currentRunningApp;

    public static DialogCloseApkViewUtils getInstance(){
        if(dialogViewUtils == null){
            dialogViewUtils = new DialogCloseApkViewUtils();
        }
        return dialogViewUtils;
    }

    public void initView(Context context,String packageName){
        mContext = context;
        openPackageName = packageName;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_dialog_close_apk, null);
            tvTitle = inflate.findViewById(R.id.tv_title);
            tvInfo = inflate.findViewById(R.id.tv_info);
            tvLeft = inflate.findViewById(R.id.tv_left);
            tvRight = inflate.findViewById(R.id.tv_right);
            clBg = inflate.findViewById(R.id.cl_bg);
            iv1 = inflate.findViewById(R.id.iv1);
            iv2 = inflate.findViewById(R.id.iv2);
            iv3 = inflate.findViewById(R.id.iv3);

        }
        ivs.add(iv1);
        ivs.add(iv2);
        ivs.add(iv3);

        tvLeft.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        clBg.setOnClickListener(this);
        iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);
        iv3.setOnClickListener(this);

        inirApk();

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        layoutParams.gravity = Gravity.CENTER;

        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            inflate.setVisibility(View.VISIBLE);
        }

    }

    public void inirApk(){
        RunningAppUtils.getInstance().init(mContext);
        currentRunningApp = RunningAppUtils.getInstance().getCurrentRunningApp();
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa    currentRunningApp.size  : "+currentRunningApp.size());
        for (int i = 0;i< currentRunningApp.size();i++){
            ImageLoadUtils.getInstance(mContext).setImageUtlRadius(ivs.get(i),currentRunningApp.get(i).getIcon(),0,11);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cl_bg:
                goneLayout();
                break;
            case R.id.tv_left:
                goneLayout();

                break;
            case R.id.tv_right:
                break;
            case R.id.iv1:
                DialogViewUtils.getInstance().setClosePackageName(currentRunningApp.get(0).getAppid());
                DialogViewUtils.getInstance().setOpenPackageName(openPackageName);
                DialogViewUtils.getInstance().setIsCloseDialog(true);
                DialogViewUtils.getInstance().initView(mContext,false,DialogViewUtils.EXIT_APP);
                goneLayout();
                break;
            case R.id.iv2:
                DialogViewUtils.getInstance().setClosePackageName(currentRunningApp.get(1).getAppid());
                DialogViewUtils.getInstance().setOpenPackageName(openPackageName);
                DialogViewUtils.getInstance().setIsCloseDialog(true);
                DialogViewUtils.getInstance().initView(mContext,false,DialogViewUtils.EXIT_APP);
                goneLayout();
                break;
            case R.id.iv3:
                DialogViewUtils.getInstance().setClosePackageName(currentRunningApp.get(2).getAppid());
                DialogViewUtils.getInstance().setOpenPackageName(openPackageName);
                DialogViewUtils.getInstance().setIsCloseDialog(true);
                DialogViewUtils.getInstance().initView(mContext,false,DialogViewUtils.EXIT_APP);
                goneLayout();
                break;

        }
    }

    public String getPackageName(){
        return packageName;
    }

    public void setPackageName(String packageName){
        this.packageName = packageName;
    }



    public void goneLayout(){
        if(windowManager != null){
            if(inflate != null){
                inflate.setVisibility(View.GONE);
            }
        }

    }

    public void invaliView(){
        if(inflate != null){
            windowManager.removeView(inflate);
            isFist = true;
            windowManager = null;
            inflate = null;
        }
    }

}
